home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Twin / startSlaveProcess.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  110 lines

  1. static char copyright[] = "Copyright 1990 by The MITRE Corporation.";
  2. /* John D. Ramsdell - June 1990
  3.  *
  4.  * Copyright 1990 by The MITRE Corporation
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 1, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  * 
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #import "startSlaveProcess.h"
  22. #import <appkit/appkit.h>
  23. #import <appkit/Application.h>
  24. #import <defaults/defaults.h>
  25. #import <stdio.h>
  26. #import <stdlib.h>
  27. #import <string.h>
  28. #import <libc.h>
  29. #import <dpsclient/dpsclient.h>
  30.  
  31. /* Creates a slave process and the pipes used to do the communication. */
  32.  
  33. static int pid;
  34.  
  35. static void processInHandler(int fd, void *userData)
  36. {                /* Handles input from the slave process. */
  37.   static char buf[BUFSIZ + 1]; int n; 
  38.   n = read(fd, buf, BUFSIZ);
  39.   if (n < 0) {
  40.     fprintf(stderr, "\nError in processIn reading %d bytes.\n", n);
  41.     perror ("input from pipe");
  42.     [NXApp terminate:((id) userData)];
  43.   } 
  44.   else if (n == 0) [NXApp terminate:((id) userData)];
  45.   else {
  46.     char *textString;
  47.     textString = malloc(n + 1);
  48.     buf[n] = '\0';
  49.     [((EvalDelegate *) userData) showString:strcpy(textString, buf)];
  50.   } 
  51. }
  52.  
  53. int interruptSlaveProcess(void)
  54. {
  55.   return kill(pid, SIGINT);
  56. }
  57.  
  58. const char shellScript[] =
  59.   "if $0\n"
  60.   "then\n"
  61.   "  exit 0\n"
  62.   "else\n"
  63.   "  echo Slave exited with $?.\n"
  64.   "  echo For slave command: $0.\n"
  65.   "  echo\n"
  66.   "  echo Starting a shell.\n"
  67.   "  exec /bin/sh -i\n"
  68.   "fi\n"
  69.   "exit 1\n";
  70.  
  71. static void createProcess(int out[2])
  72. {
  73.   int in[2]; const char *slaveCommand;
  74.  
  75.   slaveCommand = NXGetDefaultValue("Twin", "SlaveCommand");
  76.   if (slaveCommand == NULL) slaveCommand = "# Could not get default";
  77.   if (-1 == pipe (in)) perror("Pipe error");
  78.   if (-1 == pipe (out)) perror ("Pipe error");
  79.  
  80.   if (0 == (pid = fork())) {
  81.     close (0);
  82.     close (1);
  83.     if (0 != dup (in[0])) perror("dup error");
  84.     if (1 != dup (out[1])) perror("dup error");
  85.     close (2);
  86.     if (2 != dup (1)) exit(1);
  87.     close (in[0]); close (in[1]); close (out[0]); close (out[1]);
  88.  
  89. #if defined DEBUG
  90.     write (2, shellScript, strlen(shellScript)); /* show script. */
  91.     write (2, slaveCommand, strlen(slaveCommand));
  92.     write (2, "\n\n", 2);
  93. #endif
  94.  
  95.     execl("/bin/sh", "-sh", "-c", shellScript, slaveCommand, NULL);
  96.     exit(1);
  97.   }
  98.   close (in[0]); close (out[1]);
  99.   out[1] = in[1];
  100. }
  101.  
  102. FILE *startSlaveProcess(EvalDelegate *evalDelegate)
  103. {                /* Creates the slave process, */
  104.   int fildes[2];        /* adds processInHandler to handle */
  105.   createProcess(fildes);    /* data from the slave, and returns */
  106.   DPSAddFD(fildes[0], processInHandler,    /* the file pointer used to */
  107.        (void *)evalDelegate, NX_RUNMODALTHRESHOLD);    /* send */
  108.   return fdopen(fildes[1], "a"); /* data to the slave process. */
  109. }
  110.